﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace FirstHomeWork.Utility.Extend
{
    public static class RemarkExtend
    {
        /// <summary>
        /// 扩展方法，获得枚举的Remark
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetRemark(this Enum value)
        {
            string str = value.ToString();

            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
                return null;
            FieldInfo field = type.GetField(name);
            if (field.IsDefined(typeof(RemarkAttribute), true))
            {
                var remarkAttribute = field.GetCustomAttribute<RemarkAttribute>();
                return remarkAttribute.Remark;
            }
            return str;
        }

        /// <summary>
        /// 根据枚举类型获取字典信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="keyDefault"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetRemark<T>(string keyDefault = "", string valueDefault = "")
        {
            Dictionary<string, string> dicEnum = new Dictionary<string, string>();

            Type enumType = typeof(T);
            if (!enumType.IsEnum)
                return null;

            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项              
                dicEnum.Add(keyDefault, valueDefault);
            
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                if (field.IsDefined(typeof(RemarkAttribute), true))
                {
                    var remarkAttribute = field.GetCustomAttribute<RemarkAttribute>();
                    description = remarkAttribute.Remark;
                }
                else
                {
                    description = item; //描述不存在取字段名称  
                }
                //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
                dicEnum.Add(description, ((int)Enum.Parse(enumType, item)).ToString());
            }
            return dicEnum;
        }

        /// <summary>
        /// 获取枚举的元素描述信息
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <param name="keyDefault">默认key值</param>
        /// <param name="valueDefault">默认value值</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetDescriptions(this Type type, string keyDefault = "", string valueDefault = "")
        {
            Dictionary<string, string> dicEnum = new Dictionary<string, string>();
            Type enumType = type;
            if (!enumType.IsEnum)
            {
                return null;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                if (field.IsDefined(typeof(RemarkAttribute), true))
                {
                    var remarkAttribute = field.GetCustomAttribute<RemarkAttribute>();
                    description = remarkAttribute.Remark;
                }
                else
                {
                    description = item;  //描述不存在取字段名称  
                }
                //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
                dicEnum.Add(description, ((int)Enum.Parse(enumType, item)).ToString());
            }
            return dicEnum;
        }
    }
}
